import requests
import base64

# 步骤1: 读取图片文件并转换为Base64编码
with open('captcha1.png', 'rb') as image_file:
    image_bytes = image_file.read()
    image_base64 = base64.b64encode(image_bytes).decode('utf-8')  # 将图片字节转换为Base64编码字符串，并解码为utf-8格式的字符串

# 步骤2: 构造JSON数据
json_data = {
    "ImageBase64": image_base64
}

# 步骤3: 发送POST请求
api_url = "http://127.0.0.1:6688/api.Image_to_Text"
resp = requests.post(api_url, json=json_data)

print(resp.content)
# 检查响应状态码
if resp.status_code == 200:
    # 解析响应内容为JSON
    response_data = resp.json()
    # 假设API返回的文本在JSON的"Text"键中
    text = response_data.get('result', '')
    print("识别的文本:", text)
else:
    print("请求失败，状态码:", resp.status_code)